package com.zhongyouyun.admin.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zhongyouyun.ZhongyouyunApplication;
import com.zhongyouyun.admin.model.FileModel;
import com.zhongyouyun.admin.pojo.FilePojo;
import com.zhongyouyun.other.file.Bean.FileBean;
import com.zhongyouyun.other.file.Bean.FileBeanWork;
import com.zhongyouyun.other.file.FileImp;
import com.zhongyouyun.other.model.Result;
import com.zhongyouyun.utils.IPUtils;
import com.zhongyouyun.utils.ImageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhongyouyun.admin.service.FileService;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;


/**
 * @Author: shenyi
 * @Date: 2022-04-29
 * @Description:
 */
@RestController
@RequestMapping("/admin/z_file")
@Api(value = "文件管理", tags = {"文件管理接口"})
public class FileController {
    private final static Logger logger = LoggerFactory.getLogger(FileController.class);
    @Autowired
    private FileService service;

    // 单个文件一起上传
    @PostMapping(value = "/upload")
    @ApiOperation(value = "单文件上传", notes = "只能上传一个文件")
    public Result upload(@RequestParam("file") MultipartFile file) {
        //注意参数
//        System.out.println(file.isEmpty());
        FileBean fileBean = FileImp.ImpUploadFile(file);
        if (0 == fileBean.getState()) {
            FileModel fileModel = new FileModel();
            BeanUtil.copyProperties(fileBean, fileModel);
            String url = "";
            Boolean flag = ImageUtil.isImage(fileModel.getExtensionName());
            if (flag) {
                url = "/apiupload/" + "image/" + fileModel.getUrl() + "/" + fileModel.getFileName() + fileModel.getExtensionName();
            } else {
                url = "/apiupload/" + "file/" + fileModel.getUrl() + "/" + fileModel.getFileName() + fileModel.getExtensionName();
            }
            fileModel.setRamark(url);
            service.save(fileModel);
            return Result.builder().message(fileBean.getMessage()).data(fileModel).status(0).build();
        }
//        System.out.println("---------------------");
        return Result.builder().message(fileBean.getMessage()).success(false).status(1).build();
    }



    // 单个文件一起上传
    @GetMapping(value = "/getSSS")
    @ApiOperation(value = "", notes = "只能上传一个文件")
    public Result getSSS() {    //注意参数

        System.out.println("---------------------");
        return Result.builder().data("123456").success(false).status(1).build();

    }

    // 单个文件一起上传
    @PostMapping(value = "/uploadE")
    @ApiOperation(value = "图文编辑器单文件上传", notes = "只能上传一个文件")
    public Result uploadE(@RequestParam("file") MultipartFile file, HttpServletRequest request) {    //注意参数
//        System.out.println(file.isEmpty());
        FileBean fileBean = FileImp.ImpUploadFile(file);
        if (0 == fileBean.getState()) {
            FileModel fileModel = new FileModel();
            BeanUtil.copyProperties(fileBean, fileModel);
            Boolean flag = ImageUtil.isImage(fileModel.getExtensionName());
            String url = "";
            if (flag) {
                url = "/apiupload/" + "image/" + fileModel.getUrl() + "/" + fileModel.getFileName() + fileModel.getExtensionName();
            } else {
                url = "/apiupload/" + "file/" + fileModel.getUrl() + "/" + fileModel.getFileName() + fileModel.getExtensionName();
            }
            fileModel.setRamark(url);
            service.save(fileModel);
            FilePojo filePojo = new FilePojo();
            filePojo.setHref(url);
            filePojo.setSrc(url);
            return Result.builder().message(fileBean.getMessage()).data(filePojo).errno(0).build();
        }
        System.out.println("---------------------");
        return Result.builder().message(fileBean.getMessage()).success(false).status(1).build();

    }

    // 单个html文件一起上传
    @PostMapping(value = "/uploadHtml")
    @ApiOperation(value = "上传生成html", notes = "只能上传一个Html")
    public Result uploadHtml(@ApiParam(name = "html代码", value = "html", required = true) @RequestParam(value = "html") String html) {    //注意参数
//        System.out.println(file.isEmpty());
        String url = FileImp.ImpUploadHtml(html);
        if (ObjectUtil.isNotEmpty(url)) {
            url = "/apiupload/"+url;
            return Result.builder().message("生成html成功").data(url).errno(0).build();
        }
        return Result.builder().message("生成html失败").success(false).status(1).build();
    }

    // 单个html文件一起上传
    @PostMapping(value = "/deleteHtml")
    @ApiOperation(value = "删除html", notes = "删除多个html")
    public Result deleteHtml(@RequestBody List<String> list) {    //注意参数
//        System.out.println(file.isEmpty());
        Boolean flag = FileImp.deleteHtml(list);
        if (flag) {
            return Result.builder().message("删除成功").errno(0).build();
        }
        return Result.builder().message("删除失败").success(false).status(1).build();
    }

    // 多个文件一起上传
    @PostMapping("/uploadhandleFile")
    @ApiOperation(value = "多文件上传", notes = "多个包含一个上传一个文件")
    public Result uploadhandleFile(HttpServletRequest request) {   //注意参数
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        FileBeanWork fileBeanWork = FileImp.ImpUploadhandleFile(files);
        if (0 == fileBeanWork.getState()) {
            List<FileModel> fileModelList = new ArrayList<FileModel>();
            fileBeanWork.getList().forEach(item -> {
                FileModel fileModel = new FileModel();
                BeanUtil.copyProperties(item, fileModel);
                fileModelList.add(fileModel);
            });
            service.saveBatch(fileModelList);
            return Result.builder().message(fileBeanWork.getMessage()).data(fileModelList).status(0).build();
        }
        return Result.builder().message(fileBeanWork.getMessage()).success(false).status(1).build();
    }

    /**
     * 文件下载
     *
     * @return
     */

    @GetMapping(value = "/downloadFile")
    @ApiOperation(value = "下载文件", notes = "一个文件下载")
    public void downloadFile(@NonNull @ApiParam(value = "文件ID") @RequestParam("id") Long id, HttpServletRequest request, HttpServletResponse response) {
        FileModel fileModel = new FileModel();
        fileModel.setId(id);
        service.ImpdownloadFile(fileModel, request, response);

    }


    /**
     * 文件删除
     *
     * @return
     */
    @PostMapping(value = "/delFile")
    @ApiOperation(value = "删除文件", notes = "一个文件删除")
    public Result delFile(@NonNull @ApiParam(value = "文件ID") @RequestParam("id") int id) {
        FileBeanWork fileBeanWork = service.delFile(id);
        if (0 == fileBeanWork.getState()) {
            return Result.builder().message(fileBeanWork.getMessage()).status(0).build();
        }
        return Result.builder().message(fileBeanWork.getMessage()).success(false).status(1).build();
    }
}
